package DAL;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entities.ConnectionManager;
import entities.InActiveUser;


public class InActiveUserDA {

	public InActiveUser[] selectInActiveUsers(InActiveUser user)
	{
		boolean whereClause = false;
		String query = "SELECT * from InActiveUser  " ;
		if(user.getId() != -1)
		{
			query += "WHERE id = " + user.getId();
			whereClause = true;
		}
				
		if(!user.getEmail().equals("")) 
		{
			if(whereClause)
				query += " AND email = '" + user.getEmail() + "'";
			else
				query += " WHERE email = '" + user.getEmail() + "'";
			whereClause = true;
		}
		if(!user.getEmailHash().equals("")) 
		{
			if(whereClause)
				query += " AND EmailHash = '" + user.getEmailHash() + "'";
			else
				query += " WHERE EmailHash = '" + user.getEmailHash() + "'";
			whereClause = true;
		}

		ConnectionManager con = new ConnectionManager();
		ResultSet rs = con.executeSelectQuery(query);
		
		ArrayList<InActiveUser> users = new ArrayList<InActiveUser>();

		
		try {
			while(rs.next())
			{
				InActiveUser tempUser= new InActiveUser();
				tempUser.setId(rs.getInt("id"));
				tempUser.setEmail(rs.getString("email"));
				users.add(tempUser);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		InActiveUser [] temp = new InActiveUser [0];
		return users.toArray(temp);

	}


	public boolean updateInActiveUser(InActiveUser user)
	{
		boolean setClause = false;
		String query = "UPDATE InActiveUser SET ";
		if(!user.getEmail().equals("")) 
		{
			query += " email = '" + user.getEmail() + "'  ";
			setClause = true;
		}
		if(!user.getEmailHash().equals("")) 
		{
			if(setClause)
				query += " , EmailHash = '" + user.getEmailHash() + "'";
			else
				query += " EmailHash = '" + user.getEmailHash() + "'";
			setClause = true;
		}
		query += " WHERE id = "+ user.getId();
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return (x==1);
	}
	
	public boolean insertInActiveUser(InActiveUser user)
	{
		String query = "INSERT INTO InActiveUser (email, EmailHash) VALUES " +
				"('" + user.getEmail() + "' , '"+user.getEmailHash() + "' )";
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		return x==1;
	}
	
	public int deleteInActiveUser(InActiveUser user)
	{
		String query = "DELETE FROM InActiveUser ";
		boolean whereClause = false;
		if(user.getId() != -1)
		{
			query += "WHERE id = " + user.getId();
			whereClause = true;
		}
				
		if(!user.getEmail().equals("")) 
		{
			if(whereClause)
				query += " AND email = '" + user.getEmail() + "'";
			else
				query += " WHERE email = '" + user.getEmail() + "'";
			whereClause = true;
		}
		if(!user.getEmailHash().equals("")) 
		{
			if(whereClause)
				query += " AND EmailHash = '" + user.getEmailHash() + "'";
			else
				query += " WHERE EmailHash = '" + user.getEmailHash() + "'";
			whereClause = true;
		}
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x;
	}
	
	
	public static void main(String[] args) {
		InActiveUser u = new InActiveUser();

		//u.setId(3);
		u.setEmail("asdsad");
		u.setEmailHash("asdaaaaaaasadasd");

		InActiveUserDA da = new InActiveUserDA();
		System.out.println(da.deleteInActiveUser(u));
	}

}
